package cn.xyafu.lost.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.xyafu.lost.R;
import cn.xyafu.lost.pojo.Lost;

/**
* @author wangdh
* @time 2021/12/9 0009
* desc： ListView的身体的适配器
*/
public class ListViewBodyAdapter extends BaseAdapter {
    private final Context context;  // 上下文
    private final List<Lost> list; //数据源

    public ListViewBodyAdapter(Context context, List<Lost> list) {
        this.context = context;
        this.list = list;
    }

    //listView的总条目数
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }
    //根据位置返回当前的条目
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    //当前条目的id
    @Override
    public long getItemId(int position) {
        return position;
    }


    /**
     * //根据位置返回当前条目的视图
     * @param position  : 当前位置
     * @param convertView : 复用的view , 被拖出去的view,然后会被复用的view,也是getView返回的对象
     * @param parent : 父节点
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 根据不同的类型, 来处理不同的视图(item)

        Lost lost = (Lost) getItem(position);

        // 1. 获取当前条目的类型

        ViewHolder viewHolder = null;
        if(convertView == null){ // 没有加载过,那么就创建
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.home_item_layout,parent,false);
            viewHolder.tvTitleView = (TextView) convertView.findViewById(R.id.item_title_view);
            viewHolder.tvFooterView = (TextView) convertView.findViewById(R.id.item_footer_view);
            viewHolder.tvTimeView = (TextView) convertView.findViewById(R.id.item_time_view);
            viewHolder.tvShowLost = (TextView) convertView.findViewById(R.id.tv_show_lost);
            viewHolder.tvLostId = (TextView) convertView.findViewById(R.id.item_lid_view);
            convertView.setTag(viewHolder);

        }else{//加载过,那么就获取之前的
           viewHolder = (ViewHolder) convertView.getTag();
        }
        // (3). 给组件添加数据
        viewHolder.tvTitleView.setText(lost.getLtitle());
        viewHolder.tvFooterView.setText(lost.getLmsg());
        viewHolder.tvTimeView.setText(lost.getLdate());
        viewHolder.tvLostId.setText(String.valueOf(lost.getLid()));

        return convertView;
    }

    /**
     * ViewHolder
     */
    class ViewHolder {

        TextView tvTimeView;
        TextView tvFooterView;
        TextView tvTitleView;

        TextView tvLostId;
        TextView tvShowLost;

    }
}
